var error_text = ('<span class="error">*</span>');
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		$('#add_user').submit( function() {
			var flag_submit = true;
			$("span.error").remove();

			if ($.trim($("#uname").val()) == "") {
			  $("#uname").after(error_text);
			  flag_submit = false;
			}
			if ($.trim($("#upass").val()) == "") {
			  $("#upass").after(error_text);
			  flag_submit = false;
			}
			if ($.trim($("#ufullname").val()) == "") {
			  $("#ufullname").after(error_text);
			  flag_submit = false;
			}
			if ($.trim($("#uemail").val()) == "" || !emailReg.test($.trim($("#uemail").val()))) {
			  $("#uemail").after(error_text);
			  flag_submit = false;
			}
			
			if(flag_submit){
				var content = $('#add_user').serializeArray();
				$.ajax({
					type: 'GET',
					url: './module/user/function.php',
					data: {act:'new', val:content},
					success: function(data) {
						if(data==0){
							alert ('This username is duplicate');
						} else if(data==1) {
							$('#add_user').html('<span class="success_span">One user has been created</span>');
						} else if(data==2) {
							alert ('ERROR Cannot save data');
						} else{
							alert ('Unknown Error \n'+ data );
						}
					},
					error: function (){ alert ('Cannot get function file');}
				});
				return true;
			}
			return false;
		}); 

		$('#changeinfo_user').submit( function() {
			var flag_submit = true;
			$("span.error").remove();

			if ($.trim($("#ufullname").val()) == "") {
			  $("#ufullname").after(error_text);
			  flag_submit = false;
			}
			if ($.trim($("#uemail").val()) == "" || !emailReg.test($.trim($("#uemail").val()))) {
			  $("#uemail").after(error_text);
			  flag_submit = false;
			}
			
			if(flag_submit){
				var content = $('#changeinfo_user').serializeArray();
				$.ajax({
					type: 'GET',
					url: './module/user/function.php',
					data: {act:'editinfo', val:content},
					success: function(data) {
						if(data==0){
							alert ('ERROR cannot update information');
						} else if(data==1) {
							$('#changeinfo_user').html('<span class="success_span">Your information has been saved</span>');
						} else{
							alert ('Unknown Error \n'+ data );
						}
					},
					error: function (){ alert ('Cannot get function file');}
				});
				return true;
			}
			return false;
		}); 

		$('#changepass_user').submit( function() {
			var flag_submit = false;
			$("span.error").remove();

			if ($.trim($("#ucurpass").val()) == "") {
			  $("#ucurpass").after(error_text);
			  flag_submit = true;
			}
			if ($.trim($("#unewpass").val()) == "") {
			  $("#unewpass").after(error_text);
			  flag_submit = true;
			}
			if ($.trim($("#uretype").val()) == "") {
			  $("#uretype").after(error_text);
			  flag_submit = true;
			}
			if ($.trim($("#unewpass").val()) != $.trim($("#uretype").val())) {
			  $("#uretype").after(error_text);
			  flag_submit = true;
			}

			if(!flag_submit){
				var content = $('#changepass_user').serializeArray();
				$.ajax({
					type: 'GET',
					url: './module/user/function.php',
					data: {act:'changepass', val:content},
					success: function(data) {
						if(data==0){
							alert ('Wrong ID/Password');
						} else if(data==1) {
							$('#changepass_user').html('<span class="success_span">Saved new password</span>');
						} else if(data==1) {
							alert ('ERROR cannot update your password');
						} else{
							alert ('Unknown Error \n'+ data );
						}
					},
					error: function (){ alert ('Cannot get function file');}
				});
			}
		}); 

		$('#changepermission_user').submit( function() {
				var content = $('#changepermission_user').serializeArray();
				$.ajax({
					type: 'GET',
					url: './module/user/function.php',
					data: {act:'changepermission', val:content},
					success: function(data) {
						if(data==0){
							alert ('Wrong ID/Password');
						} else if(data==1) {
							$('#changepermission_user').html('<span class="success_span">Saved permission of user</span>');
						} else if(data==1) {
							alert ('ERROR cannot update psermission');
						} else{
							alert ('Unknown Error \n'+ data );
						}
					},
					error: function (){ alert ('Cannot get function file');}
				});
		}); 
